(function () {
    var origin = window.location.origin
    var sendTimer = ''
   /*登陸*/
    $('#signIn').click(function () {

        register()

    })

    /*图表*/
    var initEchart = function (id,x,y) {
        var myChart = echarts.init(document.getElementById(id));
        var option = {

            xAxis: {
                type: 'category',
                data: x,
                axisLabel: {
                    interval:0,
                    rotate:270,
                    inside: false,
                    textStyle: {
                        color: '#38BAFF'
                    }
                },
                axisTick: {
                    alignWithLabel: true,
                    interval:0
                },
            },
            yAxis: {
                type: 'value',
                name: '吞吐量（万吨）',
                axisLabel: {
                    textStyle: {
                        color: '#999'
                    }
                }
            },

            series: [{
                data: y,
                type: 'bar',
                itemStyle: {
                    normal: {color: '#38BAFF'},

                },
                barGap:'10%',
                label:{
                    show:true,
                    position:'top'
                },
                barWidth: '',
            }]
        };
        myChart.setOption(option);
    }
    var allcountrydata = JSON.parse($('#left-echart').attr('data'))
    var maincountrydata = JSON.parse($('#right-echart').attr('data'))
    initEchart('left-echart',
        allcountrydata.map(function(_){return _.port}),
        allcountrydata.map(function(_){return _.throughput})
    )
    initEchart('right-echart',
        maincountrydata.map(function(_){return _.port}),
        maincountrydata.map(function(_){return _.throughput})
    )
    $('#loginbtn').click(function () {
        $.ajax({
            url: origin + "/api/login",
            dataType:'json',
            data:{
                email: document.getElementById('input-email').value,
                password: document.getElementById('input-password').value
            },
            xhrFields: {
                withCredentials: true
            },
            success: function(res){
                if(res.code === 200){
                }else {
                    alert(res.msg)
                }
            },
            complete:function (res) {
            }
        });
    })
    function register() {
        var loading = layer.load(1, {
            shade: [.6,'#fff']
        });
        $.ajax({
            url:  origin + "/api/webregister",
            type:"post",
            data:{
                email: document.getElementById('register-email').value,
                password: document.getElementById('register-pwd').value,
                code: document.getElementById('register-code').value
            },
            success: function(res){
                layer.msg(res)
                if(res.code === 200){
                    setTimeout(function () {
                        window.location.reload()
                    },2000)
                }
            },
            fail(){
                layer.msg('注册失败')
            },
            complete:function (res) {
                layer.close(loading);
            }
        });
    }
    $('#sendMail').click(function() {
        if(!document.getElementById('register-email').value) return
        var now = new Date().getTime()
        if(sendTimer && (now - sendTimer <60000)){
            layer.msg('发送过于频繁')
            return
        }else {
            sendTimer = now
        }
        $.ajax({
            url:  origin + "/api/sendCode",
            type:"get",
            data:{
                email: document.getElementById('register-email').value,
            },
            success: function(res){
                layer.msg(res)
                layer.msg('已发送')
            },
            fail(){
                layer.msg('发送失败')
            },
            complete:function (res) {
            }
        });
    })

})()
